package lrscp.lib.CmdUtils.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import lrscp.lib.Log;

public class StreamReadThread extends Thread {
	private static final boolean DEBUG = false;

	Object isRet = new Object();

	InputStream in = null;
	StringBuffer out = null;

	public StreamReadThread(String name, InputStream in, StringBuffer out, Object isRet) {
		super(name);
		this.in = in;
		this.out = out;
		this.isRet = isRet;
	}

	@Override
	public void run() {
		String line = null;
		try {
			// android use utf-8, so decode with it
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));// ,
																																						// "utf-8"
			while ((line = reader.readLine()) != null) {
				if (DEBUG) {
					Log.i("stream read " + line);
				}
				out.append(line);
				out.append('\n');
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		synchronized (isRet) {
			isRet.notifyAll();
		}
		if (DEBUG) {
			Log.i("stream read over notify:" + getName());
		}
	}
}